package com.zjdiepu.www.manage.api.file;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.base.lang.OSSCallbackBody;
import com.zjdiepu.www.commpent.VerifyOSSCallback;
import com.zjdiepu.www.ibiz.base.IOtherAttachmentsService;
import com.zjdiepu.www.model.base.dto.OtherAttachments;

/**
 * 项目名称：dp-rest <br/> 
 * 类名称：OSSCallbackController <br/>  
 * 类描述：oss操作回调地址<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年3月30日 上午9:36:41  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年3月30日 上午9:36:41   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Controller
@RequestMapping(value = "OSSCallbackController", produces = "application/json;charset=UTF-8")
public class OSSCallbackController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(OSSCallbackController.class);
	
	@Autowired
	private VerifyOSSCallback verifyOSSCallback;
	@Autowired
	private IOtherAttachmentsService otherAttachmentsService;
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: verify <br/>
	 * @Description: 回调通知 <br/>
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @version V1.0 <br/>
	 */
	@RequestMapping(value = "callback", method = { RequestMethod.POST })
	public void callback(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String ossCallbackBodyJson  = verifyOSSCallback.getPostBody(
						request.getInputStream(),
						Integer.parseInt(request.getHeader("Content-Length")));
			boolean verify = verifyOSSCallback.verifyOSSCallbackRequest(request,
					ossCallbackBodyJson);
			if (verify) {
				OSSCallbackBody ossCallbackBody = JSON.parseObject(ossCallbackBodyJson, OSSCallbackBody.class);
				OtherAttachments otherAttachments = new OtherAttachments();
				otherAttachments.setId(ossCallbackBody.getDataId());
				otherAttachments.setFileSize((int)ossCallbackBody.getSize());
				otherAttachments.setFileHandleStatus("01");
				otherAttachmentsService.updateJust(otherAttachments);
				verifyOSSCallback.response(request, response,
						"{\"Status\":\"OK\"}", HttpServletResponse.SC_OK);
			} else {
				verifyOSSCallback.response(request, response,
						"{\"Status\":\"verdify not ok\"}",
						HttpServletResponse.SC_BAD_REQUEST);
			}
		} catch (NumberFormatException e) {
			logger.error("callback NumberFormatException.", e);
		} catch (IOException e) {
			logger.error("callback IOException.", e);
		} catch (HttpClientException e) {
			logger.error("callback HttpClientException.", e);
		}
	}
	
}
